<?php

class Model_MailOutbox extends RL_Model
{
	public $template = NULL;

	public $from = 'postmaster@platonica.ru';
	public $subject = '';

	protected function init()
	{
		$this->store = $this->ctx->store;
		$td = $this->ctx->td;
		$outbox = $td->new_inst('cmailoutbox');
		$outbox->store = $this->ctx->store;
		$this->list = $outbox;
	}

	public function post ($message)
	{
		$outbox = $this->list;

		$t[   'from'] = $this->buildFrom    ($message);
		$t[     'to'] = $this->buildTo      ($message);
		$t[   'text'] = $this->buildText    ($message);
		$t[   'html'] = $this->buildHtml    ($message);
		$t['subject'] = $this->buildSubject ($message);

		$this->store->db->begin();
		$mail = $this->store->create('fmail', $t);
		$mail->save();
		$status = $outbox->add($mail);
		$this->store->db->end();
	}
	public function send ($o, $list)
	{
		require_once 'htmlMimeMail5/htmlMimeMail5.php';

		$from    = $o->data['from'];
		$to      = $o->data['to'];
		$subject = $o->data['subject'];
		$text    = $o->data['text'];
		$html    = $o->data['html'];

		$mail = new HtmlMimeMail5();
		$mail->setFrom($from);
		$mail->setReturnPath($this->from);
		$mail->setSubject($subject);
		$mail->setHTMLEncoding(new QPrintEncoding());
		$mail->setTextEncoding(new QPrintEncoding());
		$mail->setTextWrap(60);
		$mail->setHTMLCharset('UTF-8');
		$mail->setTextCharset('UTF-8');
		$mail->setHeadCharset('UTF-8');

		if ($text) $mail->setText($text);
		if ($html) $mail->setHtml($html);
		$to = is_array($to) ? $to : array($to);
		$to = array_map(array($this, 'quoteEmail'), $to);

		$status = $mail->send($to, 'mail');

		if ($status)
		{
			if ($this->delete_after_send)
			{
				$list->del($o);
			}
			else
			{
				$o->data['state'] = '1';
				$list->sync($o);
			}
		}

	} // end of method save

	public function buildFrom ($message)
	{
		$from = isset($message['from']) ? $message['from'] : $this->from;

		if (isset($message['poster'])) {
			$poster_name  = $message['poster']['data']['fullname'];
			$poster_email = $message['poster']['data']['email'];
			//$poster_email = strtr($poster_email, array('@' => ' at ', '.' => ' dot '));
			$email_title = $poster_name . ($poster_email ? ' - ' . $poster_email : '');
			$email_title = $this->quoteEmailLocalPart($email_title);
		}
		$from = ($email_title ? $email_title .' ' : '') . $this->quoteEmail($from);
		return $from;
	} // end of method buildFrom
	public function buildTo ($message)
	{
		$to = $message['to'];
		/*
		$to = is_array($this->to) ? $this->to : array($this->to);
		$to = array_map(array(&$this, 'quoteEmail'), $to);
		 */
		return $to;
	} // end of method buildTo
	public function buildSubject ($message)
	{
		$subject = $message;
		$this->ctx->tpl->set('*', $subject);
		$out = trim($this->ctx->tpl->Parse($this->template.':subject'));
		return $out ? $out : $this->subject;
	}
	public function buildText ($message)
	{
		$this->ctx->tpl->load($message);
		$out = trim($this->ctx->tpl->Parse($this->template.':text'));
		return $out ? $out : NULL;
	}
	public function buildHtml ($message)
	{
		$this->ctx->tpl->load($message);
		$out = trim($this->ctx->tpl->Parse($this->template.':html'));
		return $out ? $out : NULL;
	}

	public function quoteEmailLocalPart ($email_title)
	{
		if (empty($email_title)) return '';

		//$email_title = preg_replace('#[^-[:alnum:]!\\#$%*/?|^{}`~&\'+=_]#', ' ', $email_title);
		$email_title = strtr($email_title, '"', '');
		$email_title = '"'.$email_title.'"';
		return $email_title;
	} // end of method quoteEmailLocalPart
	public function quoteEmail ($email)
	{
		return '<'.$email.'>';
	}

}
